package com.gree.framework.service.impl;

import com.gree.common.service.impl.BaseServiceImpl;
import com.gree.common.utils.StringUtils;
import com.gree.framework.entity.SysClient;
import com.gree.framework.service.SysClientService;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 180551 on 2019/6/15.
 */
@Service
public class SysClientServiceImpl extends BaseServiceImpl<SysClient> implements SysClientService {

    private final String RESETPWD = "123456";
    private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    public SysClientServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public SysClient findByClientId(String clientId) {
        SysClient sysClient = this.fetch(Cnd.where("clientId", "=", clientId));
        return sysClient;
    }

    @Override
    public List<SysClient> selectSysClient(SysClient sysClient) {
        Cnd cnd = Cnd.NEW();
        if(StringUtils.isNotEmpty(sysClient.getClientId())){
            cnd = Cnd.NEW().where("clientid","like",sysClient.getClientId());
        }
        return this.query(cnd);
    }


    @Override
    public int updateSysClient(SysClient sysClient) {
        return this.update(sysClient);
    }

    @Override
    public int deleteSysClientById(String clientId) {
        return this.clear(Cnd.where("clientid","=",clientId));
    }

    @Override
    public int addSysClient(SysClient sysClient) {
        SysClient sysClient2 = this._insert(sysClient);
        if(sysClient2 != null){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public int resetPassword(String clientId) {
        return this.update(Chain.make("PWD",encoder.encode(RESETPWD)),Cnd.where("clientid","=",clientId));
    }
}
